<!-- 订单管理 -->
<template>
  <div class="order-header">
    <el-row
      style="height: 70px"
      :gutter="10"
      type="flex"
      justify="space-between"
      align="middle"
    >
      <el-col :span="4">
        <el-form-item label="订单号">
          <el-input v-model="orderNumber" placeholder="请输入订单号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item label="收货人">
          <el-input v-model="receiver" placeholder="请输入收货人"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item label="手机号">
          <el-input v-model="phoneNumber" placeholder="请输入手机号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item label="订单状态">
          <el-select v-model="orderStatus" placeholder="全部">
            <el-option label="全部" value=""></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已受理" value="已受理"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="下单时间">
          <el-time-picker
            v-model="orderTime"
            is-range
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          >
          </el-time-picker>
        </el-form-item>
      </el-col>
      <el-col :span="2">
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </div>
  <div class="order-content">
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="orderNo" label="订单号"></el-table-column>
      <el-table-column prop="orderTime" label="下单时间"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="consignee" label="收货人"></el-table-column>
      <el-table-column prop="deliverAddress" label="配送地址"></el-table-column>
      <el-table-column prop="deliveryTime" label="送达时间"></el-table-column>
      <el-table-column prop="remarks" label="用户备注"></el-table-column>
      <el-table-column prop="orderAmount" label="订单金额"></el-table-column>
      <el-table-column prop="orderState" label="订单状态"></el-table-column>
      <el-table-column label="操作" width="125">
        <template #default="scope">
          <el-button
            type="text"
            size="small"
            @click="handleLook(scope.$index, scope.row)"
            >查看</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- .查看对话框 -->
    <el-dialog
      center
      lock-scroll
      draggable
      title="订单详情"
      v-model="lookDialogVisible"
      width="30%"
      @close="lookDialogVisible = false"
    >
      <div v-for="column in columns" :key="column.prop">
        <div class="dialog-item">
          {{ column.label }} <span>：</span> {{ currentRow[column.prop] }}
        </div>
      </div>
      <template #footer>
        <el-button
          style="
            background-color: #409eff;
            color: #fff;
            width: 80px;
            height: 40px;
          "
          @click="lookDialogVisible = false"
          >确定</el-button
        >
      </template>
    </el-dialog>
    <!-- .编辑对话框 -->
    <el-dialog
      center
      lock-scroll
      draggable
      title="订单编辑"
      v-model="editDialogVisible"
      width="30%"
      @close="editDialogVisible = false"
    >
      <div v-for="column in columns" :key="column.prop">
        <div class="dialog-item item">
          <div class="dialog-left">{{ column.label }}：</div>
          <div class="dialog-right">
            <template
              v-if="
                column.prop === 'orderNo' ||
                column.prop === 'orderTime' ||
                column.prop === 'deliveryTime' ||
                column.prop === 'orderAmount'
              "
            >
              <span>{{ currentRow[column.prop] }}</span>
            </template>
            <template v-else>
              <el-input v-model="currentRow[column.prop]"></el-input>
            </template>
          </div>
        </div>
      </div>
      <template #footer>
        <el-button
          style="
            background-color: #409eff;
            color: #fff;
            width: 80px;
            height: 40px;
          "
          @click="handleSave"
          >确定修改</el-button
        >
      </template>
    </el-dialog>
    <!-- .分页器 -->
    <el-pagination
      style="margin-top: 10px"
      background
      layout="total, sizes,prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
      >
    </el-pagination>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { getOrderList, editOrderList, searchOrderList } from "@/api/index.js";
import { onMounted } from "vue";
import { format } from "date-fns"; // 日期工具库
import { ElMessage } from "element-plus";

const orderNumber = ref(""); // 订单号(search)
const receiver = ref(""); // 收货人(search)
const phoneNumber = ref(""); // 手机号(search)
const orderStatus = ref(""); // 订单状态(search)
const orderTime = ref(""); // 下单时间(search)["2019-10-05 00:00:00","2019-10-10 00:00:00"]
const tableData = ref([]); // 订单列表数据
const total = ref(20); // 总数据量
const pageSize = ref(7); // 每页显示的数据量(search)
const currentPage = ref(1); // 当前页码(search)
const lookDialogVisible = ref(false); // 对话框是否可见
const editDialogVisible = ref(false); // 对话框是否可见
const currentRow = ref({}); // 当前行数据
// 列定义，包含表头和对应的数据键
const columns = ref([
  { label: "订单号", prop: "orderNo" },
  { label: "下单时间", prop: "orderTime" },
  { label: "手机号", prop: "phone" },
  { label: "收货人", prop: "consignee" },
  { label: "配送地址", prop: "deliverAddress" },
  { label: "送达时间", prop: "deliveryTime" },
  { label: "用户备注", prop: "remarks" },
  { label: "订单金额", prop: "orderAmount" },
  { label: "订单状态", prop: "orderState" },
]);

// 查询
const search = () => {
  console.log(orderStatus.value);
  searchOrderFn();
};

// 编辑操作
const handleEdit = (index, row) => {
  currentRow.value = { ...row };
  editDialogVisible.value = true;
};

// 修改保存操作
const handleSave = () => {
  editDialogVisible.value = false;
  console.log("🚀 ~ handleEdit ~ currentRow:", currentRow.value);
  editOrderFn(currentRow.value);
};

// 查看操作
const handleLook = (index, row) => {
  console.log("🚀 ~ handleLook ~ row:", row);
  currentRow.value = { ...row };
  lookDialogVisible.value = true;
};

// 获取订单管理列表请求
const getOrderListFn = async () => {
  const res = await getOrderList({
    currentPage: currentPage.value,
    pageSize: pageSize.value,
  });
  // console.log("订单管理列表", res);
  // 转换日期格式
  if (res && res.data) {
    res.data.forEach((item) => {
      item.orderTime = formatDate(item.orderTime);
      item.deliveryTime = formatDate(item.deliveryTime);
    });
    tableData.value = res.data;
  }
};

//编辑订单请求
const editOrderFn = async () => {
  const res = await editOrderList(currentRow.value);
  console.log("编辑订单", res);
  if (res.code === 0) {
    getOrderListFn(); // 重新获取订单列表
    ElMessage({
      message: "编辑成功",
      type: "success",
      duration: 1000,
    });
  } else {
    ElMessage({
      message: "编辑失败",
      type: "error",
      duration: 1000,
    });
  }
};

//查询订单请求
const searchOrderFn = async () => {
  const res = await searchOrderList({
    orderNo: orderNumber.value,
    consignee: receiver.value,
    phone: phoneNumber.value,
    orderState: orderStatus.value,
    date: orderTime.value,
    currentPage: currentPage.value,
    pageSize: pageSize.value,
  });
  console.log("查询订单", res);
  if (res && res.data) {
    res.data.forEach((item) => {
      item.orderTime = formatDate(item.orderTime);
      item.deliveryTime = formatDate(item.deliveryTime);
    });
    tableData.value = res.data; // 更新当前页的数据
    total.value = res.total; // 更新总数据量
    localStorage.setItem("orderList", "search");
  }
  if (res.total === 0) {
    ElMessage({
      message: "暂无满足查询条件的数据",
      type: "warning",
      duration: 1000,
    });
  }
};

// 分页器页码改变时的处理函数
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage;
  localStorage.getItem("orderList") === "search"
    ? searchOrderFn()
    : getOrderListFn();
};

// 每页条目数改变时的处理函数
const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  currentPage.value = 1; // 重置为第一页
  getOrderListFn();
};

// 日期格式处理函数  ISO 8601 标准的日期时间
const formatDate = (dateString) => {
  return format(new Date(dateString), "yyyy-MM-dd HH:mm:ss");
};

onMounted(() => {
  getOrderListFn();
});
</script>

<style scoped lang="less">
::v-deep .el-button--small {
  padding: 0;
}
.order-header {
  height: 70px;

  width: 100%;
}
.order-content {
  width: 100%;
}
.dialog-item {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  height: 40px;
  list-style: 30px;
  width: 100%;
  border-bottom: 1px solid #dddddd;
}

.item {
  border: none;
  .dialog-left {
    justify-content: end;
    display: flex;
    align-items: center;
    height: 80%;
    width: 20%;
  }
  .dialog-right {
    height: 80%;
    width: 80%;
    span {
      line-height: 31px;
    }
  }
}
</style>
